package com.lanxiao.cloud.modules.gateway.route.application;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.gateway.api.dto.GatewayRouteDubboDto;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRoutePageDto;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteQuery;
import com.lanxiao.cloud.modules.gateway.route.application.assembler.GatewayRouteAssembler;
import com.lanxiao.cloud.modules.gateway.route.application.query.RouteLambdaQuery;
import com.lanxiao.cloud.modules.gateway.route.application.query.RouteLambdaQueryWrapper;
import com.lanxiao.cloud.modules.gateway.route.domain.model.GatewayRoute;
import com.lanxiao.cloud.modules.gateway.route.infrastructure.repository.GatewayRoutesMapper;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-08-19
 */
@Component
public class GatewayRouteQueryManager implements ILog<GatewayRoute, Long> {

  private final GatewayRoutesMapper gatewayRoutesMapper;
  private final GatewayRouteAssembler gatewayRouteAssembler;

  public GatewayRouteQueryManager(
      GatewayRoutesMapper gatewayRoutesMapper,
      GatewayRouteAssembler gatewayRouteAssembler) {
    this.gatewayRoutesMapper = gatewayRoutesMapper;
    this.gatewayRouteAssembler = gatewayRouteAssembler;
  }

  /***
   *  查询所有路由信息
   *
   * @author ：qiaoliang

   * @return List<GatewayRouteDto>
   * @date 2021-09-09 11:52
   */
  public List<GatewayRouteDubboDto> queryAll(){
    LambdaQueryWrapper<GatewayRoute> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(GatewayRoute::getDelFlag, DelFlagEnum.NOT_DELETE);
    return gatewayRouteAssembler.toGatewayRouteDubboDto(
        gatewayRoutesMapper.selectList(queryWrapper));
  }

  /**
   *  根据条件查询路由信息
   *
   * @author ：qiaoliang
   * @param gatewayRouteQuery : 查询条件
   * @param pageQuery : 分页信息
   * @return com.lanxiao.cloud.common.core.dto.ResultModel<com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRoutePageDto>
   * @date 2021-09-09 17:27
   */
  public ResultModel<GatewayRoutePageDto> query(GatewayRouteQuery gatewayRouteQuery, PageQuery pageQuery){
    LambdaQueryWrapper<GatewayRoute> queryWrapper = RouteLambdaQueryWrapper.wrapper()
        .queryWrapper(RouteLambdaQuery.toRouteLambdaQuery(gatewayRouteQuery))
        .eqDelFlag(DelFlagEnum.NOT_DELETE)
        .geTime(pageQuery.getTime())
        .orderByAsc(GatewayRoute::getSort)
        .query();
    return gatewayRouteAssembler.toGatewayRoutePageDto(gatewayRoutesMapper.selectPage(
        new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper));
  }

  @Override
  public GatewayRoute selectById(Long id) {
    return gatewayRoutesMapper.selectById(id);
  }

}
